const app = getApp()
const { toDecimal2 } = require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    foods: [],
    totalprice: '',
    isdoing: false,
    isShow: false,
    remark: '',
    isFirstTouch: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.setData({
      isShow: app.isShowTotal
    })
  },

  onShow: function () {
    this.setData({
      foods: wx.getStorageSync('^orders$') || [],
      userinfo: wx.getStorageSync('logininfo')
    })
    this.comptedTotalPrice()
  },

  comptedTotalPrice() {
    let list = this.data.foods
    let sum = list.reduce(function (accumulator, currentValue) {
      return accumulator + parseFloat(currentValue.count) * parseFloat(currentValue.price);
    }, 0)
    this.setData({
      totalprice: sum
    })
  },
  ordergoods(e) {
    const {
      item
    } = e.currentTarget.dataset
    app.order.add(item)

    let list = this.data.foods
    list.forEach(li => {
      console.log(li, 'li')
      if (li.id == item.id) {
        li.count + 1
      }
    });
    this.setData({
      foods: list
    })
    this.comptedTotalPrice()
  },
  decrementgoods(e) {
    const {
      item
    } = e.currentTarget.dataset
    app.order.delete(item)

    let list = this.data.foods
    list.forEach(li => {
      if (li.id == item.id) {
        li.count - 1
      }
    });
    this.setData({
      foods: list
    })
    this.comptedTotalPrice()
  },

  clearShoplist() {
    app.order.clear()
    this.setData({
      foods: []
    })
    app.order.list = []

    this.comptedTotalPrice()
  },


  async wxpayIt(data) {
    const res = await app._http({
      url: "wxpay/getWxParams",
      method: 'post',
      data
    });
    if (res.data.code !== 1) {
      return wx.showToast({
        title: '提示',
        content: '创建订单失败~'
      })
    }
    // console.log(res, "res");

    wx.requestPayment({
      timeStamp: res.data.data.timeStamp,
      nonceStr: res.data.data.nonceStr,
      package: res.data.data.package,
      signType: res.data.data.signType,
      paySign: res.data.data.paySign,
      success: async res => {
        console.log(res, 'success');
        app.order.clear()
        app.order.list = []
        wx.navigateTo({
          url: '/pages/orderdetail/orderdetail?id=' + data.out_trade_no,
        })
      },
      fail: res => {
        console.log(res, 'fail');
      },
      complete: res => {
        console.log(res, 'complete');
      }
    })

  },

  async confirmBuy() {
    this.setData({
      isFirstTouch: false
    })
    if (!app.globalData.isLogin) {
      return wx.showToast({
        title: '请登录',
        icon: 'none'
      })
    }

    if (this.data.foods.length <= 0) {
      return wx.showToast({
        title: '请先点好您需要的食物~',
        icon: 'none'
      })
    }

    let data = {
      foods: JSON.stringify(this.data.foods),
      price: parseInt(toDecimal2(this.data.totalprice) * 100),
      openid: this.data.userinfo.openid,
      name: this.data.userinfo.nickName,
      store: app.store,
      remark: this.data.remark
    }
    
    let res = await app._http({
      url: 'order/createOrder',
      data,
      method: 'post'
    })
    if (res.data.code === 1) {
      console.log(res.data.data, 'res.data.data');
      let detail = res.data.data
      const data = {
        out_trade_no: detail.order_no,
        openid: detail.openid,
        total_fee: detail.price
      }
      console.log(data, 'data');

      this.wxpayIt(data)
    }


  },

  remarkOnChange(e){
    this.setData({
      remark: e.detail.value
    })
  }

})